package pers.dawnyang.modular.system.controller;

import java.util.List;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import pers.dawnyang.common.core.annotation.OperLog;
import pers.dawnyang.common.core.domain.Result;
import pers.dawnyang.common.core.domain.entity.SysMenu;
import pers.dawnyang.common.core.enums.OperType;
import pers.dawnyang.common.core.enums.ResultEnum;
import pers.dawnyang.common.core.util.ResultUtil;
import pers.dawnyang.config.exception.BusinessException;
import pers.dawnyang.modular.system.domain.request.menu.MenuReqC;
import pers.dawnyang.modular.system.domain.request.menu.MenuReqR;
import pers.dawnyang.modular.system.domain.request.menu.MenuReqU;
import pers.dawnyang.modular.system.domain.response.menu.MenuDtree;
import pers.dawnyang.modular.system.domain.response.menu.MenuInfo;
import pers.dawnyang.modular.system.domain.response.menu.MenuRes;
import pers.dawnyang.modular.system.service.MenuService;

/**
 * 菜单管理
 * 
 * @author dawn yang
 * 
 */
@RequestMapping("/sys/menu")
@Controller
public class MenuController {

  @Autowired
  private MenuService menuService;

  @SaCheckPermission("sys:menu:index")
  @GetMapping("/index")
  public String index() {
    return "/sys/menu/index";
  }

  @SaCheckPermission("sys:menu:index")
  @GetMapping("/icon")
  public String icon() {
    return "/sys/menu/icon";
  }

  @SaCheckPermission("sys:menu:add")
  @GetMapping("/addView")
  public String addView() {
    return "/sys/menu/addView";
  }

  @SaCheckPermission("sys:menu:edit")
  @GetMapping("/editView")
  public String editView(@RequestParam("id") Long id, ModelMap model) {
    MenuInfo menuInfo = menuService.getMenuInfo(id);
    model.put("model", JSON.toJSONString(menuInfo));
    return "/sys/menu/editView";
  }

  @SaCheckPermission("sys:menu:list")
  @PostMapping("/list")
  @ResponseBody
  public Result<List<MenuRes>> getList(MenuReqR req) {
    List<MenuRes> resList = menuService.getList(req);
    return ResultUtil.success(resList);
  }

  @SaCheckPermission({"sys:menu:add", "sys:menu:edit"})
  @GetMapping("/getDtreeList")
  @ResponseBody
  public Result<List<MenuDtree>> getDtreeList() {
    List<MenuDtree> resList = menuService.getDtreeList();
    return ResultUtil.success(resList);
  }

  @OperLog(operModul = "菜单管理", operType = OperType.INSERT)
  @SaCheckPermission("sys:menu:add")
  @PostMapping("/add")
  @ResponseBody
  public Result<String> add(@Validated MenuReqC req) {
    boolean flag = menuService.checkParentType(req.getParentId());
    if (!flag) {
      throw new BusinessException(ResultEnum.ERROR_MENUTYPE);
    }
    SysMenu sysMenu = new SysMenu();
    BeanUtils.copyProperties(req, sysMenu);
    menuService.save(sysMenu);
    return ResultUtil.success();
  }

  @OperLog(operModul = "菜单管理", operType = OperType.UPDATE)
  @SaCheckPermission("sys:menu:edit")
  @PostMapping("/edit")
  @ResponseBody
  public Result<String> edit(@Validated MenuReqU req) {
    boolean flag = menuService.checkParentType(req.getParentId());
    if (!flag) {
      throw new BusinessException(ResultEnum.ERROR_MENUTYPE);
    }
    SysMenu sysMenu = new SysMenu();
    BeanUtils.copyProperties(req, sysMenu);
    menuService.updateById(sysMenu);
    return ResultUtil.success();
  }

  @OperLog(operModul = "菜单管理", operType = OperType.DELETE)
  @SaCheckPermission("sys:menu:del")
  @PostMapping("/del")
  @ResponseBody
  public Result<String> del(@RequestParam("id") Long id) {
    SysMenu sysMenu = new SysMenu();
    sysMenu.setMenuId(id);
    sysMenu.setStatus(0);
    menuService.updateById(sysMenu);
    return ResultUtil.success();
  }

}
